package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.V10BindService;
import com.yixun.qfbao.apidto.*;
import com.yixun.qfbao.apidto.v10dto.UpdateFirsthandIntentionTradeStatusReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.ReportExternal;
import com.yixun.qfbao.model.dto.ReportExternalDto;
import com.yixun.qfbao.model.enums.DelFlagEnum;
import com.yixun.qfbao.model.enums.OperationTypeEnum;
import com.yixun.qfbao.service.ReportExternalService;
import com.yixun.qfbao.vo.GetTradeStatusChangeReqVo;
import com.yixun.qfbao.xf.model.CustomerPremises;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesVisitCountDto;
import com.yixun.qfbao.xf.model.enums.CustomerStatusEnum;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import com.yixun.qfbao.xf.service.CustomerPremisesService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @classname: GetTradeStatusChangeHander.java
 * @author: Beau
 * @create: 2019/11/22 10:40
 * @version: 1.0.0
 **/
@Service("getTradeStatusChangeHander")
public class GetTradeStatusChangeHander extends CommonAbstractHander {
    private static final Logger logger = LoggerFactory.getLogger(GetTradeStatusChangeHander.class);
    @Autowired
    private CustomerPremisesService customerPremisesService;
    @Autowired
    private ReportExternalService reportExternalService;
    @Autowired
    private V10BindService v10BindService;
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetTradeStatusChangeReqVo req = (GetTradeStatusChangeReqVo) reqDto;
        BaseResponseDTO<GetTradeStatusChangeResDto> baseResDto = new BaseResponseDTO<>();
        GetTradeStatusChangeResDto resDto = new GetTradeStatusChangeResDto();
        GetTradeStatusChangeReqDto params = req.getParams();
        CustomerPremisesDto entity = new CustomerPremisesDto();

        entity.setCustomerStatus(CustomerStatusEnum.has_visited.getNo());
        List<CustomerPremises> list = customerPremisesService.selectListByDto(entity);
        List<CustomerPremisesDto> updateList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(list)){
            List<Long> idList = list.stream().map(item -> item.getId()).collect(Collectors.toList());
            List<CustomerPremisesVisitCountDto> vistiCount =  customerPremisesService.visitCount(idList);
            for(CustomerPremisesVisitCountDto dto : vistiCount ){
                CustomerPremisesDto cp = new CustomerPremisesDto();
                cp.setId(dto.getCpId());
                cp.setVisitState(Integer.parseInt(dto.getNum()) > 3 ? "4" : dto.getNum());
                updateList.add(cp);

            }
        }
        if(updateList != null){
            batchUpdate(updateList);
            baseResDto.setResult("1");
            baseResDto.setErrorMsg("执行批量更新报备");
            return baseResDto;
        }
        baseResDto.setResult("0");
        baseResDto.setErrorMsg("批量更新数据为空");
        return baseResDto;
    }

    @Async
    public void batchUpdate(List<CustomerPremisesDto> list){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        try {
            for (CustomerPremisesDto entity : list){
                customerPremisesService.saveOrUpdate(entity);
            }
        } catch (Exception e){
            logger.error("batchUpdateCustomerPremises is error: {}",e);
        }
    }
}
